package com.itheima.service.impl;

import com.itheima.mapper.BrandMapper;
import com.itheima.mapper.BrandMapper2;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.service.BrandService2;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BrandServiceImpl2 implements BrandService2 {
    //1. 创建SqlSessionFactory 工厂对象
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();


    @Override
    public List<Brand> selectAll() {
        //2. 获取SqlSession对象
        //SqlSession sqlSession = factory.openSession();
        SqlSession sqlSession=factory.openSession();
        //3. 获取BrandMapper
        //BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        BrandMapper2 mapper=sqlSession.getMapper(BrandMapper2.class);

        //4. 调用方法
        List<Brand> brands = mapper.selectAll();

        //5. 释放资源
        sqlSession.close();

        return brands;
    }

    @Override
    public void add(Brand brand) {
        //2. 获取SqlSession对象
        //SqlSession sqlSession = factory.openSession();
        SqlSession sqlSession=factory.openSession();
        //3. 获取BrandMapper
        //BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        BrandMapper2 mapper=sqlSession.getMapper(BrandMapper2.class);

        mapper.add(brand);

        sqlSession.commit();

        sqlSession.close();

    }

    @Override
    public void deleteByIds(int[] ids) {
        //2. 获取SqlSession对象
        //SqlSession sqlSession = factory.openSession();
        SqlSession sqlSession=factory.openSession();
        //3. 获取BrandMapper
        //BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        BrandMapper2 mapper=sqlSession.getMapper(BrandMapper2.class);

        mapper.deleteByIds(ids);

        sqlSession.commit();

        sqlSession.close();
    }

    @Override
    public PageBean<Brand> selectByPage(int currentPage, int pageSize) {
        //2. 获取SqlSession对象
        //SqlSession sqlSession = factory.openSession();
        SqlSession sqlSession=factory.openSession();
        //3. 获取BrandMapper
        //BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        BrandMapper2 mapper=sqlSession.getMapper(BrandMapper2.class);

        int begin=(currentPage-1)*pageSize;
        int size=pageSize;
        // 当前页数据
        List<Brand> rows=  mapper.selectByPage(begin,size);
        // 总记录数（包括所有符合条件的页的数据）
        int totalCount=mapper.selectTotalCount();

        PageBean<Brand> pageBean=new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        sqlSession.close();

        return pageBean;
    }

    @Override
    public PageBean<Brand> selectByPageAndCondition(int currentPage, int pageSize, Brand brand) {
        SqlSession sqlSession=factory.openSession();
        BrandMapper2 brandMapper2=sqlSession.getMapper(BrandMapper2.class);

        int begin=(currentPage-1)*pageSize;
        int size=pageSize;

        String brandName=brand.getBrandName();
        String companyName=brand.getCompanyName();

        if(brandName !=null && brandName.length() > 0){
            brandName="%"+brandName+"%";
            brand.setBrandName(brandName);
        }
        if(companyName !=null && companyName.length() > 0){
            companyName="%"+companyName+"%";
            brand.setCompanyName(companyName);
        }
        List<Brand> rows=brandMapper2.selectByPageAndCondition(begin,size,brand);
        int totalCount=brandMapper2.selectTotalCountByCondition(brand);

        PageBean<Brand> pageBean=new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        sqlSession.close();

        return pageBean;
    }

    @Override
    public void delete(int id) {
        SqlSession sqlSession=factory.openSession();

        BrandMapper2 brandMapper2=sqlSession.getMapper(BrandMapper2.class);

        brandMapper2.delete(id);

        sqlSession.commit();

        sqlSession.close();

    }

    @Override
    public void update(Brand brand) {
        SqlSession sqlSession=factory.openSession();

        BrandMapper2 brandMapper2=sqlSession.getMapper(BrandMapper2.class);

        brandMapper2.update(brand);

        sqlSession.commit();

        sqlSession.close();

    }

//    @Override
//    public void add(Brand brand) {
//        //2. 获取SqlSession对象
//        SqlSession sqlSession = factory.openSession();
//        //3. 获取BrandMapper
//        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//
//        //4. 调用方法
//        mapper.add(brand);
//        sqlSession.commit();//提交事务
//
//        //5. 释放资源
//        sqlSession.close();
//    }
//
//    @Override
//    public void deleteByIds(int[] ids) {
//        //2. 获取SqlSession对象
//        SqlSession sqlSession = factory.openSession();
//        //3. 获取BrandMapper
//        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//
//        //4. 调用方法
//        mapper.deleteByIds(ids);
//
//        sqlSession.commit();//提交事务
//
//        //5. 释放资源
//        sqlSession.close();
//    }
//
//    @Override
//    public PageBean<Brand> selectByPage(int currentPage, int pageSize) {
//        //2. 获取SqlSession对象
//        SqlSession sqlSession = factory.openSession();
//        //3. 获取BrandMapper
//        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//
//
//        //4. 计算开始索引
//        int begin = (currentPage - 1) * pageSize;
//        // 计算查询条目数
//        int size = pageSize;
//
//        //5. 查询当前页数据
//        List<Brand> rows = mapper.selectByPage(begin, size);
//
//        //6. 查询总记录数
//        int totalCount = mapper.selectTotalCount();
//
//        //7. 封装PageBean对象
//        PageBean<Brand> pageBean = new PageBean<>();
//        pageBean.setRows(rows);
//        pageBean.setTotalCount(totalCount);
//
//
//        //8. 释放资源
//        sqlSession.close();
//
//        return pageBean;
//    }
//
//    @Override
//    public PageBean<Brand> selectByPageAndCondition(int currentPage, int pageSize, Brand brand) {
//        //2. 获取SqlSession对象
//        SqlSession sqlSession = factory.openSession();
//        //3. 获取BrandMapper
//        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//
//
//        //4. 计算开始索引
//        int begin = (currentPage - 1) * pageSize;
//        // 计算查询条目数
//        int size = pageSize;
//
//        // 处理brand条件，模糊表达式
//        String brandName = brand.getBrandName();
//        if (brandName != null && brandName.length() > 0) {
//            brand.setBrandName("%" + brandName + "%");
//        }
//
//        String companyName = brand.getCompanyName();
//        if (companyName != null && companyName.length() > 0) {
//            brand.setCompanyName("%" + companyName + "%");
//        }
//
//
//        //5. 查询当前页数据
//        List<Brand> rows = mapper.selectByPageAndCondition(begin, size, brand);
//
//        //6. 查询总记录数
//        int totalCount = mapper.selectTotalCountByCondition(brand);
//
//        //7. 封装PageBean对象
//        PageBean<Brand> pageBean = new PageBean<>();
//        pageBean.setRows(rows);
//        pageBean.setTotalCount(totalCount);
//
//
//        //8. 释放资源
//        sqlSession.close();
//
//        return pageBean;
//    }


}
